//
//  ARSegmentController.m
//  anrigo
//
//  Created by Liu Zhen on 10/22/14.
//  Copyright (c) 2014 ft. All rights reserved.
//

#import "SegmentController.h"

@interface ARSegmentBarItem : UICollectionViewCell

@property (nonatomic, strong) UILabel *titleLabel;

@end


@implementation ARSegmentBarItem

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.titleLabel];
    }
    return self;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:self.contentView.bounds];
        _titleLabel.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = [UIColor whiteColor];
    }
    return _titleLabel;
}

@end

@interface SegmentController () <JYSlideSegmentDelegate, JYSlideSegmentDataSource>{

}
@end

@implementation SegmentController

//- (instancetype)initWithCoder:(NSCoder *)aDecoder {
//    self = [super initWithCoder:aDecoder];
//    if (self) {
//        self.viewControllers = self.childViewControllers;
//        [self.childViewControllers makeObjectsPerformSelector:@selector(removeFromParentViewController)];
//    }
//    return self;
//}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.viewControllers = self.childViewControllers;
    [self.childViewControllers makeObjectsPerformSelector:@selector(removeFromParentViewController)];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.segmentBar.backgroundColor = [UIColor whiteColor];
//    self.indicator.backgroundColor = [UIColor colorFromHexString:GreenColorHexString];

    self.delegate = self;
    self.dataSource = self;
    [self.segmentBar registerClass:[ARSegmentBarItem class] forCellWithReuseIdentifier:@"ARSegmentBarItem"];
    self.segmentBar.scrollEnabled = NO;
}

#pragma mark - JYSlideSegmentDataSource
- (NSInteger)slideSegment:(UICollectionView *)segmentBar numberOfItemsInSection:(NSInteger)section {
    return [self.viewControllers count];
}

- (UICollectionViewCell *)slideSegment:(UICollectionView *)segmentBar cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ARSegmentBarItem *segmentBarItem = [segmentBar dequeueReusableCellWithReuseIdentifier:@"ARSegmentBarItem"
                                                                                 forIndexPath:indexPath];
    segmentBarItem.backgroundColor = [UIColor whiteColor];//[UIColor colorWithRed:(CGFloat)0x56/0xff green:(CGFloat)0x58/0xff blue:(CGFloat)0x56/0xff alpha:1.0];
    UIViewController *vc = self.viewControllers[indexPath.row];
    segmentBarItem.titleLabel.text = vc.title;
    segmentBarItem.titleLabel.textColor = [UIColor blackColor];
    return segmentBarItem;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < 0 || indexPath.row >= self.viewControllers.count) {
        return;
    }
    UIViewController *controller = self.viewControllers[indexPath.row];
    if (controller.parentViewController) {
        [controller viewWillAppear:YES];
    }
    [self scrollToViewWithIndex:indexPath.row animated:YES];
}

- (CGSize)slideSegment:(UICollectionView *)segmentBar layout:(UICollectionViewLayout *)segmentBarViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeZero;
}

@end
